#include<iostream>
#include<unistd.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<cerrno>
#include<string>
#include<assert.h>
int main()
{
  int pipefd[2]={0};
  //创建管道
  int n = pipe(pipefd);
  if(n<0)
  {
    std::cout<<"pipe error,"<<errno<<":"<<strerror(errno)<<std::endl;
    return -1;
  }
  std::cout<<"pipefd[0]:"<<pipefd[0]<<std::endl;
  std::cout<<"pipefd[1]:"<<pipefd[1]<<std::endl;
  //创建子进程
  pid_t id=fork();
  assert(id!=-1);
  (void)id;
  if(id==0)
  {
    //父进程写，子进程负责读
    //子进程关闭写端
   close(pipefd[1]);
   char buffer[128];
   read(pipefd[0],buffer,sizeof(buffer));
   std::cout<<"我是子进程，接收到的消息为:"<<buffer;
   close(pipefd[0]);
   exit(0);
   
  }
  //父进程
  //关闭读端
  close(pipefd[0]);
  char* name="i am father";
  write(pipefd[1],name,strlen(name));
  close(pipefd[1]);


  return 0;
}
